// 表格
import type { ModelFieldsRow } from '#/views/onlineDesign/designModel/designModel.data';

import { nextTick, ref } from 'vue';

import { message } from 'ant-design-vue';

export const fieldSelectData = ref([
  {
    label: '精确匹配',
    value: 'EQ',
  },
  {
    label: '模糊匹配',
    value: 'LIKE',
  },
  // {
  //   label: '范围匹配',
  //   value: 'between',
  // },
]);

export const columnsData = [
  {
    title: '序号',
    dataIndex: 'serialNumber',
    key: 'serialNumber',
    align: 'center',
    width: 50,
  },
  {
    title: '字段名称',
    key: 'fieldName',
    dataIndex: 'fieldName',
    align: 'center',
    // width: 180,
  },
  {
    title: '字段注释',
    key: 'fieldAnnotation',
    dataIndex: 'fieldAnnotation',
    align: 'center',
    // width: 180,
  },
  {
    title: '字段备注',
    key: 'fieldRemark',
    dataIndex: 'fieldRemark',
    align: 'center',
    // width: 180,
  },
  {
    title: '字段类型',
    key: 'fieldType',
    dataIndex: 'fieldType',
    align: 'center',
    width: 200,
  },
  {
    title: '字段长度',
    key: 'fieldLength',
    dataIndex: 'fieldLength',
    align: 'center',
    width: 110,
  },
  {
    title: '小数点',
    key: 'fieldDecimalPoint',
    dataIndex: 'fieldDecimalPoint',
    align: 'center',
    width: 110,
  },
  {
    title: '默认值',
    key: 'fieldDefaultVal',
    dataIndex: 'fieldDefaultVal',
    align: 'center',
    width: 80,
  },
  {
    title: '主键',
    key: 'fieldPrimaryKey',
    dataIndex: 'fieldPrimaryKey',
    align: 'center',
    width: 50,
  },
  {
    title: '非空',
    key: 'fieldNotNull',
    dataIndex: 'fieldNotNull',
    align: 'center',
    width: 50,
  },
  {
    title: '唯一',
    key: 'fieldUnique',
    dataIndex: 'fieldUnique',
    align: 'center',
    width: 50,
  },
];

/**
 * 校验不能为空
 * @param record 记录
 * @param val 值
 * @param msg 提示信息
 */
export function validNotNull(record: ModelFieldsRow, val: string, msg: string) {
  if (!val) {
    message.error(`${msg}不能为空`);
    return false;
  }
  return true;
}

/**
 * 校验不能重复
 * @param record 记录
 * @param val 值
 * @param msg 提示信息
 * @param dataSource 数据源
 * @param fieldName 字段名称
 */
export function validNotRepeat(
  record: ModelFieldsRow,
  val: string,
  msg: string,
  dataSource: [],
  fieldName: string,
) {
  const isDuplicate = dataSource.value.some(
    (item) => item !== record && item[fieldName] === val,
  );
  if (isDuplicate) {
    message.error(msg);
    return false;
  }
  return true;
}

/**
 * 正则校验
 * @param record 记录
 * @param val 值
 */
export function validRegex(record: ModelFieldsRow, val: string) {
  const regex = /^[a-z_]+$/i;
  if (!regex.test(val)) {
    message.error('字段名称只能包含英文字母和下划线');
    return false;
  }
  return true;
}

export function handleFieldNameChange(
  record: ModelFieldsRow,
  val: string,
  msg: string,
  dataSource: [],
  fieldName: string,
) {
  if (validNotNull(record, val, msg) && validRegex(record, val)) {
    if (validNotRepeat(record, val, msg, dataSource, fieldName)) {
      record[fieldName] = val;
    } else {
      message.error(`${val} 值重复`);
    }
  } else {
    nextTick(() => {
      // 清空无效输入
      record.fieldName = '';
    });
  }
}
